package no.uib.hplims.views.peptides;

import java.util.List;

import no.uib.hplims.models.peptideinformation.Freezer;
import no.uib.hplims.models.peptideinformation.PeptideInformation;
import no.uib.hplims.models.peptideinformation.Storage;

import org.vaadin.appfoundation.persistence.facade.FacadeFactory;

import com.vaadin.data.Container;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Field;
import com.vaadin.ui.NativeSelect;

public class StorageView extends AbstractInformationView<Storage> {

	private static final long serialVersionUID = -336122251125220213L;

	private boolean editMode = false;

	protected StorageView(List<Storage> list, PeptideInformation pi) {
		super(Storage.class, pi);
		setCaption("Storage");

		// Dummy data
//		Freezer f = new Freezer();
//		f.setFreezerName("Freezer -80");
//		FacadeFactory.getFacade().store(f);
//		f = new Freezer();
//		f.setFreezerName("Freezer -20");
//		FacadeFactory.getFacade().store(f);
//		Storage s = new Storage();
//		s.setFreezer(f);
//		s.setConcentration("Some concentration");
//		s.setNumberOfAliquots(3);
//		list.add(s);
//		FacadeFactory.getFacade().store(s);
//
//		s = new Storage();
//		s.setFreezer(f);
//		s.setConcentration("Other concentration");
//		s.setNumberOfAliquots(7);
//		list.add(s);
//		FacadeFactory.getFacade().store(s);
		// Dummy data end

		init(list);

		addVisibleColumns();
	}

	@Override
	protected void addVisibleColumns() {

		visibleColumnIds.add("freezer");
		visibleColumnIds.add("concentration");
		visibleColumnIds.add("numberOfAliquots");

		visibleColumnLabels.add("Freezer");
		visibleColumnLabels.add("Concentration");
		visibleColumnLabels.add("# of aliquots");

		itemTable.setVisibleColumns(visibleColumnIds.toArray());
		itemTable.setColumnHeaders(visibleColumnLabels.toArray(new String[0]));

	}

	public void buttonClick(ClickEvent event) {
		if (event.getButton().equals(editButton)) {

			editMode = !editMode;
			editButtonClick(editMode);

		} else if (event.getButton().equals(newButton)) {

			Storage s = new Storage();
			FacadeFactory.getFacade().store(s);
			itemContainer.addBean(s);
			editMode = true;
			editButtonClick(editMode);
		}
	}
	
	private void editButtonClick(boolean edit) {
		itemTable.setEditable(editMode);
		itemTable.setTableFieldFactory(getFieldFactory());
		
		if (edit) {
			editButton.setCaption("Save");
		} else {
			editButton.setCaption("Edit");
			
			for (Storage s : itemContainer.getItemIds()) {
				peptideInformation.addStorage(s);
				FacadeFactory.getFacade().store(s);	
			}
			FacadeFactory.getFacade().store(peptideInformation);
			
		}
	}

	@Override
	protected DefaultFieldFactory getFieldFactory() {
		if (itemTableFieldFactory == null) {
			itemTableFieldFactory = new DefaultFieldFactory() {
				
				private static final long serialVersionUID = 7265939233272059492L;

				@Override
				public Field createField(Container container, Object itemId,
						Object propertyId, Component uiContext) {

					// Create fields by their class
					Class<?> cls = container.getType(propertyId);

					// Create a NativeSelect to select a freezer
					if (cls.equals(Freezer.class)) {

						List<Freezer> freezers = FacadeFactory.getFacade()
								.list(Freezer.class);

						NativeSelect ns = new NativeSelect();
						ns.setSizeFull();
						ns.setNullSelectionAllowed(false);
						ns.setImmediate(true);

						for (Freezer f : freezers) {
							ns.addItem(f);
						}

						Storage storage = (Storage) itemId;
						ns.setValue(storage.getFreezer());

						return ns;
					}

					// Otherwise use the default field factory
					return super.createField(container, itemId, propertyId,
							uiContext);
				}
			};
		}

		return itemTableFieldFactory;
	}

}
